package com.qianfeng.missfood.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.Category_ListView_adapter;
import com.qianfeng.missfood.category_bean.Category_Bean;
import com.qianfeng.missfood.category_bean.Category_List;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 15-8-17.
 */
public class CategoryFragment extends BaseAppFragment implements AdapterView.OnItemClickListener{
    @ViewInject(R.id.category_left_listview)
    private ListView left_listview;
    private List<String> names=new ArrayList<>();
    private HttpUtils httpUtils=new HttpUtils();
    private FragmentManager manager;
    private Category_ListView_adapter listView_adapter;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_category_layout;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ViewUtils.inject(this,view);
        listView_adapter = new Category_ListView_adapter(names,getActivity());
        left_listview.setAdapter(listView_adapter);
        left_listview.setOnItemClickListener(this);

        Category_GridView_Fragment gridView_fragment=new Category_GridView_Fragment();
        Bundle bundle=new Bundle();
        bundle.putInt("position",0);
        gridView_fragment.setArguments(bundle);
        manager = getChildFragmentManager();
        manager.beginTransaction().replace(R.id.category_right_container,gridView_fragment).commit();
    }



    @Override
    public void initData() {
        super.initData();
        String CategoryUrl="http://api.mishi.cn/categories/foreground/1_3/?_method=GET&p-pv=1.0&p-apiv=3.0&step=-1&categoryId=0";
        httpUtils.send(HttpRequest.HttpMethod.GET, CategoryUrl, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if(responseInfo!=null){
                    String result=responseInfo.result;
                    Category_Bean bean= JSONObject.parseObject(result,Category_Bean.class);
                    if(bean!=null&&bean.getCategoryBOs()!=null){
                        for (Category_List list :bean.getCategoryBOs()) {
                            names.add(list.getName());
                        }
                        listView_adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {


            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        listView_adapter.selectIndex=position;//直接更改listView_adapter的属性
        listView_adapter.notifyDataSetChanged();
        Category_GridView_Fragment gridView_fragment=new Category_GridView_Fragment();
        Bundle bundle=new Bundle();
        bundle.putInt("position",position);
        gridView_fragment.setArguments(bundle);
        manager = getChildFragmentManager();
        manager.beginTransaction().replace(R.id.category_right_container,gridView_fragment).commit();
    }
}
